<template>
  <div class="bigbox">
    <div class="leftbox bgcfff">
      <left-tree @checkChange="checkChange"></left-tree>
    </div>
    <div class="rightbox">
      <div class="top">
        <div class="left">
          <el-form
            ref="form"
            :model="form"
            label-width="40px"
            label-position="left"
          >
            <el-form-item label="月份">
              <el-date-picker
                size="small"
                v-model="form.month"
                type="month"
                placeholder="选择月"
                format="yyyy-MM"
                value-format="yyyy-MM"
              >
              </el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <div class="right">
          <Twobtn @chaxun="init" @chongzhi="reset"></Twobtn>
        </div>
      </div>
      <div class="main bgcfff">
        <TopTile
          @command="command"
          @daochu="daochu"
          @shuaxin="getList"
        ></TopTile>
        <div class="echartsbox">
          <nt-echart :loading="loading" :options="lineOptions"></nt-echart>
        </div>
        <nt-table
          v-loading="loading"
          height="auto"
          :tableData="List"
          :issize="issize"
          row-key="name"
          :tableConfig="listConfig"
          default-expand-all
          :tree-props="{ children: 'children' }"
        >
        </nt-table>
      </div>
    </div>
  </div>
</template>

<script>
import { exportexcel } from '@/utils/excel'
import LeftTree from '../components/Left-tree.vue'
import {
  getListApi,
  getLineApi
} from '@/api/Intelligence-maintenance/Auxiliary-analysis/Month-coulometry'
import dayjs from 'dayjs'
import TopTile from '@/components/TopTitle/index.vue'
import NtEchart from '@/components/nt-echart.vue'
import NtTable from '@/components/nt-table/index.vue'
export default {
  components: { LeftTree, NtEchart, NtTable, TopTile },
  name: 'YearCoulometry',
  data() {
    return {
      form: {
        month: dayjs().format('YYYY-MM')
      },
      companyNos: '',
      loading: false,
      issize: 'middle',
      List: [],
      Line: [],
      tableConfig: [
        {
          label: '用户'
        }
      ]
    }
  },
  computed: {
    lineOptions() {
      const data = this.Line.map((item) => {
        return {
          name: item.name,
          type: 'line',
          smooth: true,
          data: item.epowerList.map((i) => [
            dayjs(i.dateStr).format('D日'),
            i.value
          ])
        }
      })
      return {
        title: {
          top: '3%'
        },
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          top: '10%',
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        legend: {},
        toolbox: {},
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: data
      }
    },
    listConfig() {
      if (!this.List[0]) {
        return [
          {
            label: '用户',
            prop: 'name'
          }
        ]
      }
      const configList = this.List[0]?.epowerList?.map((i) => {
        return {
          label: i.dateStr !== '总' ? dayjs(i.dateStr).format('D日') : '总',
          prop: 'value',
          width: '150px',
          align: 'right',
          formatter: (row, column) => {
            if (column.label == '总') {
              return Number(
                row.epowerList.find((i) => i.dateStr == column.label)?.value
              ).toFixed(2)
            }
            return Number(
              row.epowerList.find(
                (i) => dayjs(i.dateStr).format('D日') == column.label
              )?.value
            ).toFixed(2)
          }
        }
      })
      configList.unshift({
        label: '用电单位',
        prop: 'name',
        width: '350px'
      })
      return configList
    }
  },
  methods: {
    command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    reset() {
      this.form.month = dayjs().format('YYYY-MM')
    },
    checkChange(Nos, nodes) {
      this.companyNos = Nos
    },
    init() {
      Promise.all([this.getList(), this.getLine()])
    },
    async getList() {
      this.loading = true
      const res = await getListApi({
        companyNos: this.companyNos,
        month: this.form.month
      })
      this.List = res.data.list
      this.loading = false
    },
    async getLine() {
      this.loading = true
      const res = await getLineApi({
        companyNos: this.companyNos,
        month: this.form.month
      })
      this.Line = res.data.list
      this.loading = false
    },
    daochu() {
      exportexcel(
        '/pio-microservice-biz/pio/month/power/analyse/get/excel',
        {
          companyNos: this.companyNos,
          month: this.form.month
        },
        '月度电量分析'
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.bgcfff {
  background-color: #fff;
}
.bigbox {
  display: flex;
  width: 100vw;
  height: 100vh;
  padding: 10px;
  .leftbox {
    width: 300px;
    padding: 20px;
    margin-right: 10px;
  }
  .rightbox {
    flex: 1;
    display: flex;
    width: 1px;
    flex-direction: column;
    .top {
      display: flex;
      align-items: center;
      min-height: 80px;
      margin-bottom: 10px;
      background-color: #fff;
      padding: 0 20px;
      justify-content: space-between;
      .el-form-item {
        margin-bottom: 0;
      }
    }
    .main {
      flex: 1;
      display: flex;
      flex-direction: column;
      width: 100%;
      padding: 10px;
      .echartsbox {
        width: 100%;
        height: 50%;
      }
    }
  }
}
</style>
